package homework1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Main {
    public static void main(String[] args) {
        Thread[] threads = new Thread[100];

        for (int i = 0; i < 100; i++) {
            String entrance = i < 50 ? "后门" : "前门";
            Employee employee = new Employee(i + 1, entrance);
            employee.generateLotteryNumbers();
            threads[i] = new Thread(employee);
            threads[i].start();
        }
    }
}

class Employee implements Runnable {
    private static final Object lock = new Object();
    private static int count = 0;
    private static int backDoorCount = 0;
    private static int frontDoorCount = 0;

    private int id;
    private String entrance;
    private List<Integer> lotteryNumbers;

    public Employee(int id, String entrance) {
        this.id = id;
        this.entrance = entrance;
        this.lotteryNumbers = new ArrayList<>();
    }

    @Override
    public void run() {
        synchronized (lock) {
            if (entrance.equals("后门")) {
                backDoorCount++;
            } else if (entrance.equals("前门")) {
                frontDoorCount++;
            }

            count++;

            System.out.println("编号为: " + id + " 的员工 从" + entrance + "入场! 拿到的双色球彩票号码是:" + lotteryNumbers);

            if (count == 100) {
                System.out.println("从后门入场的员工总共: " + backDoorCount + " 位员工");
                System.out.println("从前门入场的员工总共: " + frontDoorCount + " 位员工");
            }
        }
    }

    public void generateLotteryNumbers() {
        Random random = new Random();
        for (int i = 0; i < 7; i++) {
            lotteryNumbers.add(random.nextInt(33) + 1);
        }
    }
}

